package mo.leetcode_hot;

import java.util.HashSet;
import java.util.Set;

public class L128 {

    public int longestConsecutive(int[] nums) {
        Set<Integer> setNums = new HashSet<>();
        for (int num : nums) {
            setNums.add(num);
        }
        int max = 0;
        for (int num : nums) {
            if (!setNums.contains(num-1)) {
                int cur = num;
                int tmp = 1;
                while (setNums.contains(cur+1)) {
                    cur++;
                    tmp++;
                }
                max = Math.max(tmp, max);
                if (max > nums.length / 2 + 1) {
                    break;
                }
            }
        }
        return max;
    }

}
